// packageB/pages/addAddress/index.js
const util = require('../../../utils/addressA.js')
import Dialog from '@vant/weapp/dialog/dialog';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isdabled:false,
    addInfo:{
      id:"",
      userName:"",
      telNum:"",
      myDress:"",
      provinceName:"",
      cityName:"",
      detailInfo:"",
      ifDefault:false
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
      if(options.type=='1')return
      // console.log(JSON.parse(decodeURIComponent(options.item)))
      let item = JSON.parse(decodeURIComponent(options.item))
      this.setData({
        ['addInfo.userName']:item.userName,
        ['addInfo.telNum']:item.telNum,
        ['addInfo.myDress']:item.provinceName+item.cityName,
        ['addInfo.provinceName']:item.provinceName,
        ['addInfo.cityName']:item.cityName,
        ['addInfo.detailInfo']:item.detailInfo,
        ['addInfo.ifDefault']:item.isDefault=='1'?true:false,
        ['addInfo.id']:item.id,
        isdabled:true
      })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },
  switch1Change(e){
    // console.log(e)
    this.setData({
      ['addInfo.ifDefault']:e.detail.value
    })
  },
  //输入改变
  inputChange(e){
      // console.log(e.detail.value)
      this.setData({
        ['addInfo.userName']:e.detail.value
      })
  },
  inputChange1(e){
    // console.log(e.detail.value)
    this.setData({
      ['addInfo.telNum']:e.detail.value
    })
},
  showAddress(){
    // console.log(1)
    wx.chooseLocation({
      success: (res)=> {
        // console.log(res)
        // console.log(util.getArea(res.address))
        this.setData({
         ['addInfo.myDress']:util.getArea(res.address).Province+util.getArea(res.address).City,
         ['addInfo.provinceName']:util.getArea(res.address).Province,
         ['addInfo.cityName']:util.getArea(res.address).City,
         ['addInfo.detailInfo']:util.getArea(res.address).Country+res.name,
        })
      },
      complete(r){
        // console.log(r)
        // console.log(222)
      }
    })
  },
//保存
async saveDress(){
// console.log(this.data.addInfo)
  if(!this.data.addInfo.userName)return wx.showToast({
    title: '请输入收货人名字！',
    icon:"error"
  })
  if(!this.data.addInfo.telNum)return wx.showToast({
    title: '请输入手机号！',
    icon:"error"
  })
  if(!this.data.addInfo.myDress)return wx.showToast({
    title: '请输入地址！',
    icon:"error"
  })
  let res = await getApp().api.editAdress(this.data.addInfo)
  // console.log(res)
  if(res.code==200){
    wx.navigateBack({
      delta: 1
    })
  }
},
//删除
async deletes(){
  let res = await getApp().api.deletAdress(this.data.addInfo.id)
  // console.log(res)
  if(res.code==200){
    wx.navigateBack({
      delta: 1
    })
  }
},
  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})